home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / smaltalk / manchest.lha / MANCHESTER / manchester / 2.2 / Dhrystones.st < prev    next >
Text File  |  1993-07-24  |  14KB  |  525 lines

  1. "    NAME        Dhrystones
  2.     AUTHOR        TPH@cs.man.ac.uk
  3.     FUNCTION rough comparison benchmark for ST vs other langs 
  4.     ST-VERSIONS    2.2
  5.     PREREQUISITES     
  6.     CONFLICTS    
  7.     DISTRIBUTION      world
  8.     VERSION        1.1
  9.     DATE    22 Jan 1989
  10. SUMMARY    Dhrystones
  11.     is a transliteration from the Dhrystone benchmarks in 'C' (ugh!)
  12.     to Smalltalk.  This code is not particularly representative of typical
  13.     Smalltalk programs, but may provide a non-rigorous comparison between
  14.     Smalltalk-80 and other programming languages/environments.(2.2).TPH
  15. "!
  16. 'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:02:18 pm'!
  17.  
  18. Object subclass: #ObjectF1
  19.     instanceVariableNames: ''
  20.     classVariableNames: ''
  21.     poolDictionaries: ''
  22.     category: 'Dhrystone Benchmarks'!
  23.  
  24.  
  25. !ObjectF1 methodsFor: 'actions'!
  26.  
  27. with: charPar1 and: charPar2
  28.     "Performs some charactor comparisons, and returns
  29.      an integer (was an enum)."
  30.  
  31.     | charLoc1 charLoc2 |
  32.     charLoc1 _ charPar1.
  33.     charLoc2 _ charPar2.
  34.     (charLoc1 ~= charLoc2) ifTrue: [^1] ifFalse: [^2]! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:02:21 pm'!
  35.  
  36. Object subclass: #ObjectF2
  37.     instanceVariableNames: ''
  38.     classVariableNames: ''
  39.     poolDictionaries: ''
  40.     category: 'Dhrystone Benchmarks'!
  41.  
  42.  
  43. !ObjectF2 methodsFor: 'actions'!
  44.  
  45. strcmp: str1 with: str2
  46.     "A C strcmp look-alike. Answers with the
  47.      difference between the characters at the first
  48.      position where the strings are different."
  49.  
  50.     | i l1 l2 |
  51.     i _ 1.
  52.     l1 _ str1 size.
  53.     l2 _ str2 size.
  54.     [(str1 at: i) = (str2 at: i)] whileTrue: [
  55.         i _ i + 1.
  56.         (i > l1)
  57.             ifTrue: [(i > l2) ifTrue: [^0] ifFalse: [^(0 - (str2 at: i) asInteger)]]
  58.             ifFalse: [(i > l2) ifTrue: [^((str1 at: i) asInteger)]]
  59.     ].
  60.     ^((str1 at: i) asInteger - (str2 at: i) asInteger)!
  61.  
  62. with: strParI1 and: strParI2
  63.     "Performs some string type operations, and returns a boolean result."
  64.  
  65.     | intLoc charLoc f1 |
  66.     f1 _ ObjectF1 new.
  67.     intLoc _ 1.
  68.     [intLoc <= 1] whileTrue: [
  69.         ((f1 with: (strParI1 at: intLoc) and: (strParI2 at: (intLoc + 1)))  = 1) ifTrue: [
  70.             charLoc _ $A.
  71.             intLoc _ intLoc + 1].
  72.         ((charLoc >= $W) & (charLoc <= $Z)) ifTrue: [intLoc _ 7].
  73.         (charLoc = $X)
  74.             ifTrue: [^true]
  75.             ifFalse: [
  76.                 ((self strcmp: strParI1 with: strParI2) > 0)
  77.                     ifTrue: [intLoc _ intLoc + 7. ^true]
  78.                     ifFalse: [^false]
  79.         ]
  80.     ]! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:02:26 pm'!
  81.  
  82. Object subclass: #ObjectF3
  83.     instanceVariableNames: ''
  84.     classVariableNames: ''
  85.     poolDictionaries: ''
  86.     category: 'Dhrystone Benchmarks'!
  87.  
  88.  
  89. !ObjectF3 methodsFor: 'actions'!
  90.  
  91. with: enumParIn
  92.     "Part of the Dhrystone benchmarks.  Performs a
  93.      simple test on integers and returns a boolean.
  94.      C version used enums."
  95.  
  96.     | enumLoc |
  97.     enumLoc _ enumParIn.
  98.     (enumLoc = 3) ifTrue: [^true].
  99.     ^false
  100.  
  101.     "This would be better written as:
  102.         ^(enumLoc = 3)
  103.     but the above is a direct translation from the C program."! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:01:26 pm'!
  104.  
  105. Link subclass: #DhryRecord
  106.     instanceVariableNames: 'discr enumComp intComp stringComp '
  107.     classVariableNames: ''
  108.     poolDictionaries: ''
  109.     category: 'Dhrystone Benchmarks'!
  110.  
  111.  
  112. !DhryRecord methodsFor: 'variable access'!
  113.  
  114. discr
  115.     ^discr!
  116.  
  117. discr: anInteger 
  118.     discr _ anInteger!
  119.  
  120. enumComp
  121.     ^enumComp!
  122.  
  123. enumComp: anInteger
  124.     enumComp _ anInteger!
  125.  
  126. intComp
  127.     ^intComp!
  128.  
  129. intComp: anInteger
  130.     intComp _ anInteger!
  131.  
  132. stringComp
  133.     ^stringComp!
  134.  
  135. stringComp: aString
  136.     stringComp _ aString! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:02:07 pm'!
  137.  
  138. Object subclass: #Object6
  139.     instanceVariableNames: ''
  140.     classVariableNames: ''
  141.     poolDictionaries: ''
  142.     category: 'Dhrystone Benchmarks'!
  143.  
  144.  
  145. !Object6 methodsFor: 'actions'!
  146.  
  147. with: enumParIn
  148.     "Part of the Dhrystone benchmarks.  Uses integers instead
  149.      of C enums.  Equivalent of a switch statement.  Returns
  150.      an integer."
  151.  
  152.     | enumParOut |
  153.     enumParOut _ enumParIn.
  154.     (ObjectF3 new with: enumParIn) ifFalse: [enumParOut _ 4].
  155.     (enumParIn = 1) ifTrue: [^enumParOut _ 1].
  156.     (enumParIn = 2) ifTrue: [
  157.         (Dhrystone intGlob > 100) ifTrue: [^enumParOut _ 1]
  158.                                     ifFalse: [^enumParOut _ 4]].
  159.     (enumParIn = 3) ifTrue: [^enumParOut _ 2].
  160.     (enumParIn = 4) ifTrue: [^enumParOut].
  161.     (enumParIn = 5) ifTrue: [^enumParOut _ 3].! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:01:55 pm'!
  162.  
  163. Object subclass: #Object3
  164.     instanceVariableNames: ''
  165.     classVariableNames: ''
  166.     poolDictionaries: ''
  167.     category: 'Dhrystone Benchmarks'!
  168.  
  169.  
  170. !Object3 methodsFor: 'actions'!
  171.  
  172. doit
  173.     "Part of the Dhrystone benchmarks. Performs
  174.      some operations on LinkedLists and returns a Link."
  175.  
  176.     | parOut |
  177.     Dhrystone record notNil
  178.         ifTrue: [parOut _ Dhrystone record first]
  179.         ifFalse: [Dhrystone intGlob: 100].
  180.     parOut intComp: (Object7 new with: 10 and: (parOut intComp)).
  181.     ^parOut! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:01:45 pm'!
  182.  
  183. Object subclass: #Object1
  184.     instanceVariableNames: ''
  185.     classVariableNames: ''
  186.     poolDictionaries: ''
  187.     category: 'Dhrystone Benchmarks'!
  188.  
  189.  
  190. !Object1 methodsFor: 'actions'!
  191.  
  192. with: recordIn
  193.     "Part of the dhrystone benchmarks.  Uses a LinkedList, rather
  194.      than the 'C' structures and pointers."
  195.  
  196.     "This bit is in many ways the least satisfactory part of the
  197.      translation from 'C' to Smalltalk.  Suggestions on how it might
  198.      be improved are welcomed."
  199.  
  200.     recordIn first intComp: 5.
  201.     recordIn first nextLink intComp: (recordIn first intComp).
  202.     Object3 new doit.
  203.     (recordIn first nextLink discr = 1)
  204.         ifTrue:
  205.             [recordIn first nextLink intComp: 6.
  206.              recordIn first nextLink enumComp:
  207.                 (Object6 new with: (recordIn first nextLink enumComp)).
  208.              recordIn first nextLink intComp:
  209.                 (Object7 new with: 10 and: (recordIn first nextLink intComp))]! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:01:50 pm'!
  210.  
  211. Object subclass: #Object2
  212.     instanceVariableNames: ''
  213.     classVariableNames: ''
  214.     poolDictionaries: ''
  215.     category: 'Dhrystone Benchmarks'!
  216.  
  217.  
  218. !Object2 methodsFor: 'actions'!
  219.  
  220. with: intParIO
  221.     "Part of the Dhrystone benchmarks.  Performs some
  222.      operations on integers and characters, and returns an integer."
  223.  
  224.     | intLoc enumLoc temp |    "temp not necessary in C version."
  225.     intLoc _ intParIO + 10.
  226.     [true] whileTrue: [
  227.         (Dhrystone char1Glob = $A) ifTrue: [
  228.             intLoc _ intLoc - 1.
  229.             temp _ intLoc - Dhrystone intGlob.
  230.             enumLoc _ 1].
  231.         (enumLoc = 1) ifTrue: [^temp]
  232.         ].! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:01:58 pm'!
  233.  
  234. Object subclass: #Object4
  235.     instanceVariableNames: ''
  236.     classVariableNames: ''
  237.     poolDictionaries: ''
  238.     category: 'Dhrystone Benchmarks'!
  239.  
  240.  
  241. !Object4 methodsFor: 'actions'!
  242.  
  243. doit
  244.     "Performs part of the Dhrystone benchmark code."
  245.  
  246.     | boolLoc |
  247.     boolLoc _ ((Dhrystone char1Glob) = $A).
  248.     boolLoc _ boolLoc | (Dhrystone char1Glob).
  249.     Dhrystone char2Glob: $B! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:02:04 pm'!
  250.  
  251. Object subclass: #Object5
  252.     instanceVariableNames: ''
  253.     classVariableNames: ''
  254.     poolDictionaries: ''
  255.     category: 'Dhrystone Benchmarks'!
  256.  
  257.  
  258. !Object5 methodsFor: 'actions'!
  259.  
  260. doit
  261.     "Performs part of the Dhrystone benchmark code."
  262.  
  263.     Dhrystone char1Glob: $A.
  264.     Dhrystone boolGlob: false! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:02:11 pm'!
  265.  
  266. Object subclass: #Object7
  267.     instanceVariableNames: ''
  268.     classVariableNames: ''
  269.     poolDictionaries: ''
  270.     category: 'Dhrystone Benchmarks'!
  271.  
  272.  
  273. !Object7 methodsFor: 'actions'!
  274.  
  275. with: intParI1 and: intParI2 
  276.     "Part of the Dhrystone benchmarks.  Performs some 
  277.      arithmetic, and answers with an integer."
  278.  
  279.     | intLoc |
  280.     intLoc _ intParI1 + 2.
  281.     ^intParI2 + intLoc! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:02:13 pm'!
  282.  
  283. Object subclass: #Object8
  284.     instanceVariableNames: ''
  285.     classVariableNames: ''
  286.     poolDictionaries: ''
  287.     category: 'Dhrystone Benchmarks'!
  288.  
  289.  
  290. !Object8 methodsFor: 'actions'!
  291.  
  292. with: array1Par with: array2Par with: intParI1 with: intParI2
  293.     "Part of the Dhrystone benchmarks.  Performs some operations
  294.      on arrays."
  295.  
  296.     | intLoc intIndex |
  297.     intLoc _ intParI1 + 5.
  298.     array1Par at: intLoc put: intParI2.
  299.     array1Par at: (intLoc + 1) put: (array1Par at: intLoc).
  300.     array1Par at: (intLoc + 30) put: intLoc.
  301.     intIndex _ intLoc.
  302.     [intIndex <= (intLoc + 1)] whileTrue: [
  303.         (array2Par at: intLoc) at: intIndex put: intLoc.
  304.         intIndex _ intIndex + 1].
  305.     (array2Par at: intLoc) at: (intLoc - 1) put:
  306.         (((array2Par at: intLoc) at: (intLoc - 1)) + 1).
  307.     (array2Par at: (intLoc + 20)) at: intLoc put: (array1Par at: intLoc).
  308.     Dhrystone intGlob: 5.! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:01:38 pm'!
  309.  
  310. Object subclass: #Object0
  311.     instanceVariableNames: 'intLoc1 intLoc2 intLoc3 charLoc charIndex enumLoc string1Loc string2Loc '
  312.     classVariableNames: ''
  313.     poolDictionaries: ''
  314.     category: 'Dhrystone Benchmarks'!
  315. Object0 comment:
  316. 'This contains the outer loop of the Dhrystone benchmark
  317. code.'!
  318.  
  319.  
  320. !Object0 methodsFor: 'actions'!
  321.  
  322. doit
  323.     "Repeatedly perform the code inside the benchmark loop.
  324.      Answer with the time (in milliseconds) taken for all the loops."
  325.  
  326.     | time |
  327.     time _ Time millisecondsToRun: [
  328.         Dhrystone loopCount timesRepeat: [
  329.             Object5 new doit.
  330.             Object4 new doit.
  331.             intLoc1 _ 2.
  332.             intLoc2 _ 3.
  333.             string2Loc _ 'DHRYSTONE PROGRAM, 2''ND STRING'.
  334.             enumLoc _ 2.
  335.             Dhrystone boolGlob: (ObjectF2 new with: string1Loc and: string2Loc) not.
  336.             [intLoc1 < intLoc2] whileTrue: [
  337.                 intLoc3 _ 5 * intLoc1 - intLoc2.
  338.                 intLoc3 _ Object7 new with: intLoc1 and: intLoc2.
  339.                 intLoc1 _ intLoc1 + 1].
  340.             Object8 new
  341.                 with: Dhrystone array1Glob
  342.                 with: Dhrystone array2Glob
  343.                 with: intLoc1 with: intLoc3.
  344.             Object1 new with: Dhrystone record.
  345.             charIndex _ $A.
  346.             [charIndex <= Dhrystone char2Glob] whileTrue: [
  347.                 charIndex _ (charIndex asInteger + 1) asCharacter.
  348.                 (enumLoc = (ObjectF1 new with: charIndex and: $C)) ifTrue: [
  349.                     enumLoc _ Object6 new with: 1]].
  350.             intLoc3 _ intLoc2 * intLoc1.
  351.             intLoc2 _ intLoc3 / intLoc1.
  352.             intLoc2 _ 7 * (intLoc3 - intLoc2) - intLoc1.
  353.             intLoc1 _ Object2 new with: intLoc1
  354.         ]
  355.     ].
  356.     ^time asFloat! !
  357.  
  358. !Object0 methodsFor: 'private'!
  359.  
  360. initialize
  361.     "Sets up some of the instance variables (once only),
  362.      corresponding to locals in the 'C' version, and some class
  363.      variables, corresponding to globals in the 'C' version."
  364.  
  365.     "Pointers to next record and similar things actually
  366.      done by a LinkedList and Links, which are created
  367.      in Dhrystone class initialize."
  368.  
  369.     Dhrystone record first discr: 1.
  370.     Dhrystone record first enumComp: 3.
  371.     Dhrystone record first intComp: 40.
  372.     Dhrystone record first stringComp: 'DHRYSTONE PROGRAM, SOME STRING'.
  373.  
  374.     string1Loc _ 'DHRYSTONE PROGRAM, 1''ST STRING'.
  375.     (Dhrystone array2Glob at: 8) at: 7 put: 10! !
  376. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  377.  
  378. Object0 class
  379.     instanceVariableNames: ''!
  380.  
  381.  
  382. !Object0 class methodsFor: 'instance creation'!
  383.  
  384. new
  385.     ^super new initialize! !'From Smalltalk-80, version 2, of April 1, 1983 on 9 April 1987 at 2:01:30 pm'!
  386.  
  387. Object subclass: #Dhrystone
  388.     instanceVariableNames: 'loopTime '
  389.     classVariableNames: 'Array1Glob Array2Glob BoolGlob Char1Glob Char2Glob IntGlob LoopCount Record '
  390.     poolDictionaries: ''
  391.     category: 'Dhrystone Benchmarks'!
  392. Dhrystone comment:
  393. 'This class, and the related classes (Object0, Object1 and so
  394. on), form a tranliteration of the Dhrystone (V1.1) benchmarks from ''C''
  395. into Smalltalk--80.  Accesses to ''C'' local variables have been
  396. replaced by accesses of instance variables, or by accesses to
  397. temporary variables.  Accesses to ''C'' globals have been replaced by
  398. accesses to instance variables in other objects, using message sends.
  399. ''C'' function calls have been replaced by the creation of
  400. some objects which then execute some code.
  401.  
  402. The code is as direct a transliteration as possible; it is
  403. not particularly typical of Smalltalk code.
  404.  
  405. While this benchmark is not tailored to Smalltalk in any way,
  406. it does provide a (non-rigorous) point of comparison
  407. between Smalltalk-80 implementations.  It may also be useful when
  408. comparing Smalltalk with other systems.
  409.  
  410. '!
  411.  
  412.  
  413. !Dhrystone methodsFor: 'private'!
  414.  
  415. calculateNullLoopTime
  416.     "Calculate the time taken by a loop which does nothing."
  417.  
  418.     loopTime _ (Time millisecondsToRun: [
  419.         Dhrystone loopCount timesRepeat: []]) asFloat.! !
  420.  
  421. !Dhrystone methodsFor: 'actions'!
  422.  
  423. doit
  424.     "Perform the Dhrystone loops.  Print the result in
  425.      the System Transcript."
  426.  
  427.     | runTime benchTime |
  428.     Transcript cr; cr; show: 'Running Dhrystone benchmark (Smalltalk version)....'.
  429.     runTime _ Object0 new doit.
  430.     benchTime _ ((runTime - loopTime) / 1000).
  431.     Transcript cr; show: 'Time for ', Dhrystone loopCount printString.
  432.     Transcript show: ' passes = ',(benchTime printString),' seconds.'.
  433.     Transcript cr; show: 'This system benchmarks at '.
  434.     Transcript show: (Dhrystone loopCount / benchTime) printString.
  435.     Transcript show: ' dhrystones/second.'.! !
  436. "-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
  437.  
  438. Dhrystone class
  439.     instanceVariableNames: ''!
  440.  
  441.  
  442. !Dhrystone class methodsFor: 'class access'!
  443.  
  444. array1Glob
  445.     ^Array1Glob!
  446.  
  447. array1Glob: anArray
  448.     Array1Glob _ anArray!
  449.  
  450. array2Glob
  451.     ^Array2Glob!
  452.  
  453. array2Glob: anArray
  454.     Array2Glob _ anArray!
  455.  
  456. boolGlob
  457.     ^BoolGlob!
  458.  
  459. boolGlob: aBoolean
  460.     BoolGlob _ aBoolean!
  461.  
  462. char1Glob
  463.     ^Char1Glob!
  464.  
  465. char1Glob: aChar
  466.     Char1Glob _ aChar!
  467.  
  468. char2Glob
  469.     ^Char2Glob!
  470.  
  471. char2Glob: aChar
  472.     Char2Glob _ aChar!
  473.  
  474. intGlob
  475.     ^IntGlob!
  476.  
  477. intGlob: anInteger
  478.     IntGlob _ anInteger!
  479.  
  480. loopCount
  481.     "Answers with the number of times the loop is 
  482.      to be repeated."
  483.  
  484.     ^LoopCount!
  485.  
  486. record
  487.     "Answers with the LinkedList used instead of the structures
  488.      in the original C code."
  489.  
  490.     ^Record! !
  491.  
  492. !Dhrystone class methodsFor: 'class initialization'!
  493.  
  494. initialize
  495.     "Initialize the class variables, corresponding to globals
  496.      in the 'C' version."
  497.  
  498.     LoopCount _ 50000.    "Number of iterations used."
  499.                             "Set this value to 500000
  500.                              for much better accuracy,
  501.                              but only if you can wait all day!!"
  502.     IntGlob _ 0.
  503.     Array1Glob _ Array new: 50.
  504.     Array2Glob _ Array new: 50.
  505.     1 to: Array2Glob size do: [:each | Array2Glob at: each put: (Array new: 50)].
  506.     (Array2Glob at: 8) at: 7 put: 10.
  507.     Record _ LinkedList new.
  508.     Record addLast: DhryRecord new.
  509.     Record addLast: DhryRecord new.
  510.  
  511.     "Dhrystone initialize."! !
  512.  
  513. !Dhrystone class methodsFor: 'instance creation'!
  514.  
  515. new
  516.     "Creates a new instance of the Dhrystone benchmark."
  517.  
  518.     ^super new calculateNullLoopTime
  519.  
  520.     "Dhrystone initialize."
  521.     "Dhrystone new doit."! !
  522.  
  523. Dhrystone initialize!
  524.  
  525.